<template>
  <div id="login">
    <div class="login-box">
      <img class="chahao" src="../assets/cha.png" alt="">
      <div class="xiao-box">
        <div class="tit">无界文档</div>
        <div class="erweima">
          <img :src="imgurl" alt="">
        </div>
        <div class="zhushi">请使用微信扫码登录</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
    name:'login',
    data() {
        return {
          imgurl:'',
          share:"",
          toDetailsId:""
        }
    },
    methods: {
     
     getQueryString: function (name) {
          var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
          console.log(location)

          var aa = window.location.href.indexOf("?")
          var url=window.location.href.substring(aa,window.location.href.length); 
          console.log(url)

          var r = url.substr(1).match(reg);

          if (r != null) return decodeURI(r[2]);
          return null;
      }

    },
    mounted() {
      // console.log( window.location)
      // var idArray = window.location.href.split("=");
      // var share = idArray[1];
      // console.log(idArray.length,'share')
      // if(idArray.length==2){
        
      //   let reg=new RegExp('%3D','g')//g代表全部
      //   let newMsg=share.replace(reg,'=');
      //   this.share=newMsg
      // }


      // toDetailsIdb6b17ca994c711ea9e2900163e0d25ed

      var share = this.getQueryString('share')
      var toDetailsId = this.getQueryString('toDetailsId')
      console.log(share)

      if(share){
        let reg=new RegExp('%3D','g')//g代表全部
        let newMsg=share.replace(reg,'=');
        this.share=newMsg
      }
      if(toDetailsId){
        this.toDetailsId=toDetailsId
      }
     
      
      // http://localhost:8080/#/login??share=dIcTOMN5JocsVx4njwIQ6w==

      var ip='39.101.178.10';
      var sceneStr='scend-'+new Date().getTime() +Math.ceil(Math.random()*888888+1000000);
      var ws=new WebSocket('ws://'+ip+':8082/socketLogin/'+sceneStr)
      var that=this
      ws.onopen=function(ev){
        console.log('建立连接成功')
        
      }
      ws.onmessage=function(ev){
        var parse=JSON.parse(ev.data);
        console.log(parse)
        var status=parse.status;
        if(status==200){
          ws.onclose=function(){console.log('websocket 连接关闭')}

         
          sessionStorage.setItem('sessionKey',parse.data.sessionKey);
          sessionStorage.setItem('token',parse.data.token);
          sessionStorage.setItem('isFirst',parse.data.isFirst);

          console.log(that.share)
          if(that.share!=''){
            
            that.$axios({
              method:'get',
              url:"/loadShare?share="+ that.share,
              headers: {
                'Accept':'*/*',
                // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
              },
              
              // { 
              //   sceneStr:sceneStr,
              //   pathStr:pathStr   // 1为常用 0为非常用
              // }
            }).then((response) =>{   
              console.log(response)
              // if(response.data='您不能接收本人的分享'){
              //   that.$message({
              //       showClose: true,
              //       message: '您不能接收本人的分享',
              //       type: 'success'
              //   });
              // }else if(response.data='您已接收过该文件'){
              //   that.$message({
              //       showClose: true,
              //       message: '您已接收过该文件',
              //       type: 'success'
              //   });
              // }
              // else{
              //   that.$message({
              //       showClose: true,
              //       message: '已收下改文件至我的文件',
              //       type: 'success'
              //   });
              // }

              if (response.code == 1) {
                that.$message({
                    showClose: true,
                    message: response.data.data,
                    type: 'success'
                });
              } else {
                that.$message({
                    showClose: true,
                    message: response.data.data,
                    type: 'success'
                });
              }

              that.$router.push({path:'/'})

              
              
              
            }).catch((error) =>{
              console.log(error)  
            })
          }else if (that.toDetailsId!=''){
             that.$axios({
              method:'get',
              url:"/file/toDetails?toDetailsId="+that.toDetailsId,
              headers: {
                'Accept':'*/*',
                // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
              },
              // data:{
              //   toDetailsId:that.toDetailsId
              // }
              // { 
              //   sceneStr:sceneStr,
              //   pathStr:pathStr   // 1为常用 0为非常用
              // }
            }).then((response) =>{   
              console.log(response)
              that.$message({
                  showClose: true,
                  message: '登陆成功即将跳转详情页',
                  type: 'success'
              });
              
               that.$router.push({path: '/details',query: {fileObject: response.data }})
              
            }).catch((error) =>{
              console.log(error)  
            })
          }
          else{
            that.$router.push({path:'/'})
          }

          
         
        }
      }
      var url=this.$axios.defaults.baseURL +'/getAppletQrCode'
      
      // var pathStr=this.$axios.defaults.baseURL+'/wxUserInfo'
      var pathStr='pages/login/login'

      var formdata = new FormData();
      formdata.append("sceneStr", sceneStr);
      formdata.append("pathStr", pathStr);

      this.$axios({
        method:'post',
        url:url,
        headers: {
          'Accept':'*/*',
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        data:formdata
        // { 
        //   sceneStr:sceneStr,
        //   pathStr:pathStr   // 1为常用 0为非常用
        // }
      }).then((response) =>{   
        console.log(response)
        this.imgurl='data:image/png;base64,'+response.data
      }).catch((error) =>{
        console.log(error)  
      })
    }
}
</script>

<style scoped>
#login{
  width: 100%;
  height: 100%;
  background: rgba(4,4,4,0.50);
}
.login-box{
  width: 500px;
  height: 410px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  background: #fff;
}
.chahao{
  position: absolute;
  right: 16px;
  top: 16px;
  width: 16px;
  height: 16px;
}
.xiao-box{
  height: 340px;
  width: 332px;
  text-align: center;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.tit{
  margin-top: 34px;
  font-size: 20px;
  color: #010101;
}
.erweima{
  width: 164px;
  height: 162px;
  margin: auto;
  margin-top: 32px;
  margin-bottom: 32px;
}
.erweima img{
  width: 164px;
  height: 162px;
}
.zhushi{
  font-size: 12px;
  color: #999999;
  position: relative;
  
}


</style>

